package com.javabi.unix.parser.cpuinfo;

import java.util.ArrayList;
import java.util.List;

import com.javabi.unix.delimited.DelimitedLineProcessor;

public class CpuInfoParser extends DelimitedLineProcessor<List<CpuInfo>> {

	private final List<CpuInfo> list = new ArrayList<CpuInfo>();
	private CpuInfo latest = null;

	public CpuInfoParser() {
		super(":");

	}

	@Override
	protected boolean skipEmptyLines() {
		return false;
	}

	@Override
	public List<CpuInfo> getResult() {
		if (list.isEmpty()) {
			throw new IllegalStateException("result not parsed");
		}
		return list;
	}

	public boolean processEmptyLine(int number) {
		latest = null;
		return true;
	}

	@Override
	public boolean processLine(String key, String value, int number) {
		if (latest == null) {
			latest = new CpuInfo();
			list.add(latest);
		}
		latest.put(key, value);
		return false;
	}

}
